﻿using System;
using System.Windows.Forms;
using TimeTable.Domain.DataAccess;
using TimeTable.Domain.Entity;

namespace TimeTable.AddForms
{
    public partial class NewRoomForm : Form
    {
        public NewRoomForm()
        {
            InitializeComponent();
        }

        private void CancelButtonClick(object sender, EventArgs e)
        {
            Close();
        }

        private void OKButtonClick(object sender, EventArgs e)
        {
            try
            {
                var room = new Room
                {
                    BuildingNumber = int.Parse(buildingNumber.Text),
                    RoomNumber = int.Parse(roomNumber.Text),
                    Capacity = Convert.ToInt32(capacity.Value)
                };

                if (DataAccessConnection.Instance.RoomExists(room))
                {
                    MessageBox.Show("Room already exists!");
                }
                else
                {
                    DataAccessConnection.Instance.AddRoom(room);

                    MessageBox.Show("Room successfully added!");

                    Close();
                }
            }
            catch(Exception exception)
            {
                MessageBox.Show(exception.Message, "Error");
            }
        }
    }
}
